package com.mxnet.chongjin.supoman.fragment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.mxnet.chongjin.supoman.R;
import com.mxnet.chongjin.supoman.activity.RepairDetailActivity;
import com.mxnet.chongjin.supoman.adapter.LvOrderApplyingAdapter;
import com.mxnet.chongjin.supoman.bean.RepairRecord;
import com.mxnet.chongjin.supoman.config.Api;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * A simple {@link Fragment} subclass.
 */
public class OrderDoneFragment extends Fragment implements AdapterView.OnItemClickListener {
    private Context mContext;
    private ListView mListView;
    private LvOrderApplyingAdapter mAdapter;
    private List<RepairRecord> mList;

    public OrderDoneFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_order_done, container, false);
        mContext = getContext();
        initView(v);
        return v;
    }

    public void initView(View view) {
        mList = new ArrayList<>();
        mListView = view.findViewById(R.id.lv_order_done);
        mListView.setOnItemClickListener(this);

        OkHttpUtils.get()
                .url(Api.URL_HEADER + Api.REPAIR_LIST)
                .addParams("salerNo", "saler2018000002")
                .addParams("pageNum", "0")
                .addParams("pageSize", "10")
                .addParams("declarationStatus", "finished")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        if (!TextUtils.isEmpty(response)) {
                            try {
                                JSONObject jsonObject = new JSONObject(response);
                                boolean isSuccess = jsonObject.optBoolean("success");
                                if (isSuccess) {
                                    JSONArray data = jsonObject.optJSONArray("data");
                                    for (int i = 0; i < data.length(); i++) {
                                        RepairRecord record = new RepairRecord();
                                        JSONObject object = data.optJSONObject(i);
                                        record.setRobotNo(object.optString("robotNo"));
                                        record.setDeclarationMemo(object.optString("declarationMemo"));
                                        record.setDeclarationNo(object.optString("declarationNo"));
                                        record.setRobotImgUrl(object.optString("robotImgUrl"));
                                        record.setProjectNo(object.optString("projectNo"));
                                        record.setCreateTime(object.optLong("createTime"));
                                        record.setSalerNo(object.optString("salerNo"));
                                        record.setId(object.optString("id"));
                                        mList.add(record);
                                    }
                                    mAdapter = new LvOrderApplyingAdapter(mContext, mList);
                                    mListView.setAdapter(mAdapter);
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent();
        intent.setClass(mContext, RepairDetailActivity.class);
        RepairRecord repairRecord = (RepairRecord) parent.getAdapter().getItem(position);
        Bundle bundle = new Bundle();
        bundle.putParcelable("record", repairRecord);
        intent.putExtras(bundle);
        mContext.startActivity(intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mList = null;
        mAdapter = null;
        mListView = null;
    }
}
